package cn.com.se.sharepictrue.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.business.ImageLoader;

/**
 * Created by KIDNG on 2015/12/17.
 */
public class ShowPictureActivity extends Activity {
    public static final String IMAGES_KEY = "images_key";
    public static final String IMAGE_POSITION_KEY = "image_position_key";
    @Bind(R.id.tv_show_pic_current)
    TextView mTvShowPicCurrent;
    @Bind(R.id.tv_show_pic_total)
    TextView mTvShowPicTotal;
    @Bind(R.id.vp_show_pic_page)
    ViewPager mViewPager;
    private List<String> mImageList;
    private int position;
    private List<ImageView> mImageViewList ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_picture);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle args = null;
        if (intent != null) {
            args = intent.getExtras();
            if (args != null) {
                mImageList = args.getStringArrayList(IMAGES_KEY);
                position = args.getInt(IMAGE_POSITION_KEY);
                initView();
            }
        }
    }

    private void initView() {
        mTvShowPicTotal.setText(mImageList.size()+"");
        mTvShowPicCurrent.setText((position+1)+"");
        mImageViewList = new ArrayList<>();
        for(int i = 0;i<mImageList.size();i++){
            ImageView imageView = new ImageView(this);
            ImageLoader.getInstance(this).load(imageView,mImageList.get(i),R.mipmap.ic_placeholder);
            mImageViewList.add(imageView);
        }
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mImageViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }
            @Override
            public void destroyItem(ViewGroup view, int position, Object object) {
                view.removeView(mImageViewList.get(position));
            }
            @Override
            public Object instantiateItem(ViewGroup view, int position) {
                view.addView(mImageViewList.get(position));
                return mImageViewList.get(position);
            }
        });
        mViewPager.setCurrentItem(position);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTvShowPicCurrent.setText((position+1)+"");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
